package org.example.domain.strategy.model.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.example.types.common.Constants;

import java.util.*;
import java.util.stream.Collectors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class StrategyRuleEntity {
    private Long strategyId;
    private Long awardId;
    private int ruleType;
    private String ruleModel;
    private String ruleValue;

    public static  Map<Long, List<Long>> getRuleWeightValueFromList(List<StrategyRuleEntity> list) {
        Map<Long, List<Long>> resMap = new HashMap<>();
        list.stream().filter(strategyRuleEntity -> "rule_weight".equals(strategyRuleEntity.getRuleModel()))
            .map(StrategyRuleEntity::getRuleValue).forEach(ruleValue -> {
                String[] weights = ruleValue.split(Constants.SPACE);
                for (String weight: weights) {
                    String[] tmp = weight.split(Constants.COLON);
                    Long id = Long.valueOf(tmp[0]);
                    String value = tmp[1];
                    String[] valueList = value.split(Constants.SPLIT);
                    List<Long>  awardIdList = Arrays.stream(valueList).map(Long::valueOf).toList();
                    resMap.put(id, awardIdList);
                }
            });
        return resMap;
    }
}
